package com.ssm.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 读取配置文件
 * @author gaopengchao
 *
 */
public class SystemConfigUtil
{
    private static final String EMPLOYEE_FIELD = "/systemConfig.properties"; 
    private static SystemConfigUtil systemConfigUtil = null;
    private static Map<String, String> proMap = new HashMap<String, String>();
    
    /**
     * 配置文件属性值
     * @return
     */
    public Map<String, String> getProMap()
    {
        return proMap;
    }
    
    /**
     * 获取配置文件值
     */
    public String getProValue(String headerName)
    {
        return proMap.get(headerName);
    }
    
    /**
     * 获取指定的配置文件属性值
     * @return
     */
    public LinkedHashMap<String, String> getAppointProMap(String headerName)
    {
        Map<String,String> headerFieldRef = new LinkedHashMap<String,String>();
        String headerFields = proMap.get(headerName);
        String[] arr = headerFields.split(",");
        for (String header : arr)
        {
            String[] split = header.split(":");
            headerFieldRef.put(split[0], split[1]);
        }
        return (LinkedHashMap<String, String>) headerFieldRef;
    }
    
    /**
     * 单例管理对象
     * @return
     */
    public static SystemConfigUtil getInstance()
    {
        if (systemConfigUtil == null)
        {
            systemConfigUtil = new SystemConfigUtil();
        }
        return systemConfigUtil;
    }

    /**
     * 构造方法读取
     */
    private SystemConfigUtil()
    {
        InputStream is = null;
        Properties pro = new Properties();
        try
        {
            is = getClass().getResourceAsStream(EMPLOYEE_FIELD);
            pro.load(new InputStreamReader(is, "utf-8"));
            Enumeration<?> e = pro.propertyNames();
            while (e.hasMoreElements())
            {
                String key = (String) e.nextElement();
                String value = (String) pro.get(key);
                proMap.put(key, value);
            }

        }
        catch(IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {if(is!=null){
            	is.close();// 千万别忘了关闭资源哦！
            }
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}